﻿using RuYiAdmin.Net.Common.Global;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace RuYiAdmin.Net.Common.Utility
{
    /// <summary>
    /// Date工具类
    /// </summary>
    public class DateUtil
    {
        #region 公有方法

        /// <summary>
        /// 转化为日期
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>字符串</returns>
        public static String ParseToDate(DateTime date)
        {
            var result = String.Empty;

            //MySql = 0,SqlServer = 1,Sqlite = 2,Oracle = 3,PostgreSQL = 4,Dm = 5,Kdbndp = 6
            var type = GlobalContext.DBConfig.DBType;

            switch (type)
            {
                case 0:
                    result = ParseToMySQLDate(date);
                    break;
                case 1:
                    //Complete code when use
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                default: break;
            }
            return result;
        }

        /// <summary>
        /// 转化为时间
        /// </summary>
        /// <param name="datetime">时间</param>
        /// <returns>字符串</returns>
        public static String ParseToDateTime(DateTime datetime)
        {
            var result = String.Empty;

            //MySql = 0,SqlServer = 1,Sqlite = 2,Oracle = 3,PostgreSQL = 4,Dm = 5,Kdbndp = 6
            var type = GlobalContext.DBConfig.DBType;

            switch (type)
            {
                case 0:
                    result = ParseToMySQLDateTime(datetime);
                    break;
                case 1:
                    //Complete code when use
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                default: break;
            }
            return result;
        }

        /// <summary>
        /// 判断是否为日期
        /// </summary>
        /// <param name="obj">Object</param>
        /// <returns>bool</returns>
        public static bool IsDate(Object obj)
        {
            var result = false;

            if (obj != null)
            {
                result = Regex.IsMatch(obj.ToString(), @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
            }

            return result;
        }

        /// <summary>
        /// 判断是否为时间
        /// </summary>
        /// <param name="obj">Object对象</param>
        /// <returns>bool</returns>
        public static bool IsTime(Object obj)
        {
            var result = false;

            if (obj != null)
            {
                result = Regex.IsMatch(obj.ToString(), @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
            }

            return result;
        }

        /// <summary>
        /// 判断是否为日期+时间
        /// </summary>
        /// <param name="obj">Object对象</param>
        /// <returns>bool</returns>
        public static bool IsDateTime(Object obj)
        {
            var result = false;

            if (obj != null)
            {
                result = Regex.IsMatch(obj.ToString(), @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");
            }

            return result;
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 转化为MySQL日期
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>字符串</returns>
        private static String ParseToMySQLDate(DateTime date)
        {
            return new String($"STR_TO_DATE(‘{date}’,’%Y-%m-%d’)");
        }

        /// <summary>
        /// 转化为MySQL时间
        /// </summary>
        /// <param name="datetime">时间</param>
        /// <returns>字符串</returns>
        private static String ParseToMySQLDateTime(DateTime datetime)
        {
            return new String($"STR_TO_DATE(‘{datetime}’,’%Y-%m-%d %H:%i:%s:%fff’)");
        }

        #endregion
    }
}
